package com.qjkji.orderproject.backmanager.service.impl;


import com.qjkji.orderproject.backmanager.service.StatisticsService;
import com.qjkji.orderproject.core.entity.OrderFood;
import com.qjkji.orderproject.core.entity.StatisticsEntity;
import com.qjkji.orderproject.core.entity.enums.PaymentMethod;
import com.qjkji.orderproject.core.mapper.StatisticsMapper;
import com.qjkji.orderproject.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.*;


@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    StatisticsMapper statisticsMapper;

    @Override
    public List<StatisticsEntity> clientStatisticsForDay(Integer year,
                                                         Integer month,
                                                         Integer day) {
        LocalDate date = LocalDate.of(year, month, day);
        LocalDateTime dateTime = date.atStartOfDay();
        List<StatisticsEntity> list = new ArrayList<>();
        Integer result = statisticsMapper.selectNumberOfPeopleByDay(dateTime);
        Optional<Integer> optional = Optional.ofNullable(result);
        list.add(new StatisticsEntity(StringUtils.formatDate(date), optional.orElse(0)));
        return list;
    }

    @Override
    public List<StatisticsEntity> clientStatisticsForMonth(Integer year,
                                                           Integer month) {
        List<StatisticsEntity> list = new ArrayList<>();

        LocalDate date = LocalDate.of(year, month, 1);
        Month theMonth = date.getMonth();
        int maxLen = theMonth.maxLength();
        for (int i = 1; i <= maxLen; i++) {
            LocalDate dayOfMonth = LocalDate.of(year, month, i);
            LocalDateTime dateTime = dayOfMonth.atStartOfDay();
            Integer result = statisticsMapper.selectNumberOfPeopleByDay(dateTime);
            Optional<Integer> optional = Optional.ofNullable(result);
            list.add(new StatisticsEntity(StringUtils.formatDate(dayOfMonth), optional.orElse(0)));
        }
        return list;
    }

    @Override
    public List<StatisticsEntity> clientStatisticsForYear(Integer year) {
        List<StatisticsEntity> list = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            String yearStr = year + "-" + i;
            LocalDateTime time = LocalDateTime.of(year, i, 1, 0, 0, 0);
            Integer result = statisticsMapper.selectNumberOfPeopleByYearWithMonth(time);
            Optional<Integer> optional = Optional.ofNullable(result);

            list.add(new StatisticsEntity(yearStr, optional.orElse(0)));
        }
        return list;

    }


    @Override
    public List<StatisticsEntity> incomeStatisticsForDay(Integer year, Integer month, Integer day) {
        LocalDate date = LocalDate.of(year, month, day);
        LocalDateTime dateTime = date.atStartOfDay();
        List<StatisticsEntity> list = new ArrayList<>();

        //支付宝支付
        BigDecimal alipayPrice = ofZero(statisticsMapper.selectDecimalByDay(dateTime, PaymentMethod.ALIPAY.getType()));
        StatisticsEntity alipay = new StatisticsEntity("支付宝", alipayPrice);

        //微信支付
        BigDecimal weixinPrice = ofZero(statisticsMapper.selectDecimalByDay(dateTime, PaymentMethod.WEIXIN.getType()));
        StatisticsEntity weixin = new StatisticsEntity("微信", weixinPrice);

        //现金支付
        BigDecimal cashPrice = ofZero(statisticsMapper.selectDecimalByDay(dateTime, PaymentMethod.CASH.getType()));
        StatisticsEntity cash = new StatisticsEntity("现金", cashPrice);

        //总费
        BigDecimal price = alipayPrice.add(weixinPrice).add(cashPrice);
        StatisticsEntity all = new StatisticsEntity("总记", price);


        list.add(new StatisticsEntity(StringUtils.formatDate(date),
                Arrays.asList(alipay, weixin, cash, all)));
        return list;
    }

    @Override
    public List<StatisticsEntity> incomeStatisticsForMonth(Integer year, Integer month) {
        List<StatisticsEntity> list = new ArrayList<>();

        LocalDate date = LocalDate.of(year, month, 1);
        Month theMonth = date.getMonth();
        int maxLen = theMonth.maxLength();
        for (int i = 1; i <= maxLen; i++) {
            LocalDate dayOfMonth = LocalDate.of(year, month, i);
            LocalDateTime dateTime = dayOfMonth.atStartOfDay();

            StatisticsEntity entity = new StatisticsEntity();
            entity.setKey(StringUtils.formatDate(dayOfMonth));

            //支付宝支付
            BigDecimal alipayPrice = ofZero(statisticsMapper.selectDecimalByDay(dateTime, PaymentMethod.ALIPAY.getType()));
            StatisticsEntity alipay = new StatisticsEntity("支付宝", alipayPrice);

            //微信支付
            BigDecimal weixinPrice = ofZero(statisticsMapper.selectDecimalByDay(dateTime, PaymentMethod.WEIXIN.getType()));
            StatisticsEntity weixin = new StatisticsEntity("微信", weixinPrice);

            //现金支付
            BigDecimal cashPrice = ofZero(statisticsMapper.selectDecimalByDay(dateTime, PaymentMethod.CASH.getType()));
            StatisticsEntity cash = new StatisticsEntity("现金", cashPrice);

            //总费
            BigDecimal price = alipayPrice.add(weixinPrice).add(cashPrice);
            StatisticsEntity all = new StatisticsEntity("总记", price);

            entity.setData(Arrays.asList(alipay, weixin, cash, all));

            list.add(entity);
        }
        return list;
    }


    @Override
    public List<StatisticsEntity> incomeStatisticsForYear(Integer year) {
        List<StatisticsEntity> list = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            String yearStr = year + "-" + i;
            StatisticsEntity entity = new StatisticsEntity();
            entity.setKey(yearStr);

            LocalDateTime time = LocalDateTime.of(year, i, 1, 0, 0, 0);

            //支付宝支付
            BigDecimal alipayPrice = ofZero(statisticsMapper.selectDecimalByYearWithMonth(time, PaymentMethod.ALIPAY.getType()));
            StatisticsEntity alipay = new StatisticsEntity("支付宝", alipayPrice);

            //微信支付
            BigDecimal weixinPrice = ofZero(statisticsMapper.selectDecimalByYearWithMonth(time, PaymentMethod.WEIXIN.getType()));
            StatisticsEntity weixin = new StatisticsEntity("微信", weixinPrice);

            //现金支付
            BigDecimal cashPrice = ofZero(statisticsMapper.selectDecimalByYearWithMonth(time, PaymentMethod.CASH.getType()));
            StatisticsEntity cash = new StatisticsEntity("现金", cashPrice);

            //总费
            BigDecimal price = alipayPrice.add(weixinPrice).add(cashPrice);
            StatisticsEntity all = new StatisticsEntity("总记", price);

            entity.setData(Arrays.asList(alipay, weixin, cash, all));

            list.add(entity);
        }
        return list;
    }

    public List<StatisticsEntity> bestSales(Integer year, Integer month) {
        LocalDateTime dateTime = LocalDateTime.of(year, month, 1, 0, 0, 0);
        List<OrderFood> orderFoods =  statisticsMapper.selectBestFoodByYearWithMonth(dateTime);
        List<StatisticsEntity> list = new ArrayList<>();
        for(OrderFood orderFood: orderFoods){
            list.add(new StatisticsEntity(orderFood.getFoodName(), orderFood.getAmount()));
        }
        return list;
    }

    public List<StatisticsEntity> worstSales(Integer year, Integer month) {
        LocalDateTime dateTime = LocalDateTime.of(year, month, 1, 0, 0, 0);
        List<OrderFood> orderFoods =  statisticsMapper.selectWorstFoodByYearWithMonth(dateTime);
        List<StatisticsEntity> list = new ArrayList<>();
        for(OrderFood orderFood: orderFoods){
            list.add(new StatisticsEntity(orderFood.getFoodName(), orderFood.getAmount()));
        }
        return list;
    }

    private BigDecimal ofZero(BigDecimal decimal) {
        if (decimal == null) {
            return BigDecimal.ZERO;
        }
        return decimal;
    }


}
